from concurrent import futures
import time
import cv2
import grpc
import base64
import numpy as np
import Datas_pb2 
import Datas_pb2_grpc 
import sys


def handRecon(dst):
    print(dst)
    return 1


class SendImage(Datas_pb2_grpc.SendImageServicer):
    def __init__(self):
        pass

    def SendRequest(self, request, context):
        timer = time.clock()
        b64d = base64.b64decode(request.datas)
        dBuf = np.frombuffer(b64d, dtype=np.uint8)
        dst = cv2.imdecode(dBuf, cv2.IMREAD_COLOR)
        # reply = handRecon(dst)
        time.sleep(1)
        print('process time = ' + str(time.clock() - timer))
        return Datas_pb2.toClient(reply=1)


def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    Datas_pb2_grpc.add_SendImageServicer_to_server(SendImage(), server)
    server.add_insecure_port('[::]:50051')
    server.start()
    print('===== server start =====')

    try:
        while True:
            time.sleep(0)

    except KeyboardInterrupt:
        server.stop(0)


if __name__ == '__main__':
    serve()
